<?php

/*
  VoIP Wholesale 1.0
  ------------------
  Document   : module_mod_settings
  Created on : Sep 25, 2011, 1:28:26 PM
  Author     : Milos Jovanovic <miloscode@gmail.com>
  Description:

 */
class Module_mod_carriers extends MY_Model {
    
    function __construct() {
        parent::__construct();

		$this->MODULE->config->load('module_config');	
		$this->MODULE->short_name = $this->MODULE->config->item('short_name');
		$this->MODULE->settings_table = $this->MODULE->config->item('settings_table');	 	

		$this->MODULE->mysql_table = "carriers";
		
		$this->PACKAGE->security->load->library('forms');
		
		$this->APPLICATION->load->library('form_validation');
    }

public function search_carriers($post)
{

	$query = "SELECT * FROM `{$this->MODULE->mysql_table}` WHERE ";

	$sql = array();
	
	// country
	if($post['country_id'] != "none")
	{
		$sql[] = "`country_id` = '".$post['country_id']."'";
	}
	// username
	$username = strtoupper($post['username']);
	$default_username = strtoupper(lang('term_username'));
	if(strlen($username) > 0 && $username != $default_username)
	{
		$sql[] = "LEFT(`username`,".strlen($username).") = '".$username."'";
	}
	// status
	if($post['status'] != "none")
	{
		if($post['status'] == "all")
		{
			$sql[] = "(`status` = '0' OR `status` = '1')";
		}
		if($post['status'] == "active")
		{
			$sql[] = "(`status` = '1')";
		}
		if($post['status'] == "inactive")
		{
			$sql[] = "`status` = '0'";
		}		
	}
	// contract type
	if($post['contract_type'] != "none")
	{
		$sql[] = "`contract_type` = '".$post['contract_type']."'";
	}
	// service type
	if($post['service_type'] != "none")
	{
		if($post['service_type'] == "wholesale")
		{
			$sql[] = "`special_numbers_only` = '0'";
		}
		if($post['service_type'] == "special_services")
		{
			$sql[] = "`special_numbers_only` = '1'";
		}		
	}
	
	// max results
	if(@$post['num_of_results'] != "none" && count($sql) > 0)
	{
		$limit = " LIMIT ".$post['num_of_results'];
	}
	
	// prevent empty filters response
	if(count($sql) > 0){
	
		$sql = $query . implode(" AND ",$sql) . @$limit;
	
		//print $sql;die();

		$query = $this->db->query($sql);
	
		foreach($query->result_array() as $carrier)
		{
			$carriers[] = $carrier;
		}		
	}
	else
	{
		$carriers = $this->PACKAGE->routing->carriers->get_all_carriers();
	}
	return @$carriers;
	
}	
	
		
public function check_username($username)
{
	if(!$this->APPLICATION->form_validation->min_length($username,6))
	{
			$response = array(
							"result" => false,
							"text" => lang('msg_username_too_short')
							 );		
	}	
	elseif(!$this->APPLICATION->form_validation->alpha_dash($username))
	{
			$response = array(
							"result" => false,
							"text" => lang('msg_special_chars_in_username')
							 );			
	}
	else
	{
		$query = $this->db->where('username',$username)->get('carriers');
		
		if($query->num_rows() > 0)
		{
			$response = array(
							"result" => false,
							"text" => lang('msg_username_already_taken')
							 );
		}
		else
		{
			$response = array(
							"result" => true,
							"text" => lang('msg_username_available')
							 );
		}
	}
	return $response;
}
	

public function add_new_carrier($post)
{
	$errors = array();
	$fields = array();

   /*
    * *****************************************************************************
    * *****************************************************************************
	* VERIFICATIONS COMMON FOR ALL CONTRACT TYPES
	* *****************************************************************************
	* *****************************************************************************
	*/	
	if(!$this->APPLICATION->form_validation->required($post['contract_type']))
	{
		$errors[] = lang('term_empty') . "&nbsp;" . lang('term_contract_type');
		$fields[] = "contract_type";
	}
	
	// ###################
	// USERNAME VALIDATION
	// ###################
	if(!$this->APPLICATION->form_validation->required($post['username']))
	{
		$errors[] = lang('term_empty') . "&nbsp;" . lang('term_username');	
		$fields[] = "username";		
	}

	$check = $this->check_username($post['username']);

	if($check['result'] != "true")
	{
		$errors[] = $check['text'];
		$fields[] = "username";
	}	
	// ###################
	// PASSWORD VALIDATION
	// ###################	
	if(!$this->APPLICATION->form_validation->required($post['password']))
	{
		$errors[] = lang('term_empty') . "&nbsp;" . lang('term_password');		
		$fields[] = "password";
	}
	if(!$this->APPLICATION->form_validation->required($post['password_repeat']))
	{
		$errors[] = lang('term_empty') . "&nbsp;" . lang('term_password_repeat');	
		$fields[] = "password_repeat";		
	}
	if($post['password'] != $post['password_repeat'])
	{
		$errors[] = lang('msg_passwords_do_not_match');		
		$fields[] = "password**password_repeat";
	}
	if(!$this->APPLICATION->form_validation->min_length($post['password'],8) || !$this->APPLICATION->form_validation->min_length($post['password_repeat'],8))
	{
		$errors[] = lang('msg_password_shorter_than_8');		
		$fields[] = "password**password_repeat";
	}	
	
	// ###################
	// OTHER GLOBAL FIELDS
	// ###################	
	if(!$this->APPLICATION->form_validation->required($post['legal_name']))
	{
		$errors[] = lang('term_empty') . "&nbsp;" . lang('term_legal_name');	
		$fields[] = "legal_name";
	}	
	if(!$this->APPLICATION->form_validation->required($post['sales_name']))
	{
		$errors[] = lang('term_empty') . "&nbsp;" . lang('term_sales_name');	
		$fields[] = "sales_name";
	}
	if(!$this->APPLICATION->form_validation->required($post['sales_emails']))
	{
		$errors[] = lang('term_empty') . "&nbsp;" . lang('term_sales_emails');
		$fields[] = "sales_emails";		
	}
	if(!$this->APPLICATION->form_validation->valid_emails($post['sales_emails']))
	{
		$errors[] = lang('term_invalid') . "&nbsp;" . lang('term_sales_emails');
		$fields[] = "sales_emails";		
	}	
	if(!$this->APPLICATION->form_validation->required($post['sales_phone']))
	{
		$errors[] = lang('term_empty') . "&nbsp;" . lang('term_sales_phone');
		$fields[] = "sales_phone";		
	}
	if(!$this->APPLICATION->form_validation->required($post['billing_name']))
	{
		$errors[] = lang('term_empty') . "&nbsp;" . lang('term_billing_name');
		$fields[] = "billing_name";
	}
	if(!$this->APPLICATION->form_validation->required($post['billing_emails']))
	{
		$errors[] = lang('term_empty') . "&nbsp;" . lang('term_billing_emails');
		$fields[] = "billing_emails";
	}
	if(!$this->APPLICATION->form_validation->valid_emails($post['billing_emails']))
	{
		$errors[] = lang('term_invalid') . "&nbsp;" . lang('term_billing_emails');
		$fields[] = "billing_emails";
	}	
	if(!$this->APPLICATION->form_validation->required($post['noc_name']))
	{
		$errors[] = lang('term_empty') . "&nbsp;" . lang('term_noc_name');
		$fields[] = "noc_name";
	}
	if(!$this->APPLICATION->form_validation->required($post['noc_emails']))
	{
		$errors[] = lang('term_empty') . "&nbsp;" . lang('term_noc_emails');
		$fields[] = "noc_emails";
	}
	if(!$this->APPLICATION->form_validation->valid_emails($post['noc_emails']))
	{
		$errors[] = lang('term_invalid') . "&nbsp;" . lang('term_noc_emails');
		$fields[] = "noc_emails";
	}	
	if(!$this->APPLICATION->form_validation->required($post['noc_phone']))
	{
		$errors[] = lang('term_empty') . "&nbsp;" . lang('term_noc_phone');
		$fields[] = "noc_phone";
	}
	if(!$this->APPLICATION->form_validation->required($post['legal_emails']))
	{
		$errors[] = lang('term_empty') . "&nbsp;" . lang('term_legal_emails');
		$fields[] = "legal_emails";
	}
	if(!$this->APPLICATION->form_validation->valid_emails($post['legal_emails']))
	{
		$errors[] = lang('term_invalid') . "&nbsp;" . lang('term_legal_emails');
		$fields[] = "legal_emails";
	}	
	if(!$this->APPLICATION->form_validation->required($post['company_phone']))
	{
		$errors[] = lang('term_empty') . "&nbsp;" . lang('term_company_phone');
		$fields[] = "company_phone";
	}
	if(!$this->APPLICATION->form_validation->required($post['currency_id']) || $post['currency_id'] == "none")
	{
		$errors[] = lang('term_empty') . "&nbsp;" . lang('term_currency');	
		$fields[] = "currency";
	}
	if(!$this->APPLICATION->form_validation->required($post['address_line_1']))
	{
		$errors[] = lang('term_empty') . "&nbsp;" . lang('term_address_line_1');		
		$fields[] = "address_line_1";
	}
	if(!$this->APPLICATION->form_validation->required($post['state']))
	{
		$errors[] = lang('term_empty') . "&nbsp;" . lang('term_state');
		$fields[] = "state";
	}
	if(!$this->APPLICATION->form_validation->required($post['city']))
	{
		$errors[] = lang('term_empty') . "&nbsp;" . lang('term_city');	
		$fields[] = "city";
	}
	if(!$this->APPLICATION->form_validation->required($post['zip']))
	{
		$errors[] = lang('term_empty') . "&nbsp;" . lang('term_zip');	
		$fields[] = "zip";
	}
	if(!$this->APPLICATION->form_validation->required($post['registration_id']))
	{
		$errors[] = lang('term_empty') . "&nbsp;" . lang('term_registration_id');	
		$fields[] = "registration_id";
	}
	if(!$this->APPLICATION->form_validation->required($post['vat_id']))
	{
		$errors[] = lang('term_empty') . "&nbsp;" . lang('term_vat_id');	
		$fields[] = "vat_id";
	}	
	if(!$this->APPLICATION->form_validation->integer($post['country_id']) || $this->APPLICATION->form_validation->less_than($post['country_id'],1))
	{
		$errors[] = lang('term_empty') . "&nbsp;" . lang('term_country');	
		$fields[] = "country";
	}

	
   /*
    * *****************************************************************************
    * *****************************************************************************
	* VERIFICATIONS FOR "WE BUY FROM THEM" CONTRACT TYPE
	* *****************************************************************************
	* *****************************************************************************
	*/	
	// ###################
	// BANKING INFORMATION
	// ###################	
	if($post['contract_type'] == "we_buy_from_them" || $post['contract_type'] == "bi_lateral")
	{
	if(!$this->APPLICATION->form_validation->required($post['bank_name']))
	{
		$errors[] = lang('term_empty') . "&nbsp;" . lang('term_bank_name');
		$fields[] = "bank_name";
	}
	if(!$this->APPLICATION->form_validation->required($post['bank_address_1']))
	{
		$errors[] = lang('term_empty') . "&nbsp;" . lang('term_bank_address_1');
		$fields[] = "bank_address_1";
	}
	if(!$this->APPLICATION->form_validation->required($post['bank_city']))
	{
		$errors[] = lang('term_empty') . "&nbsp;" . lang('term_bank_city');
		$fields[] = "bank_city";
	}
	if(!$this->APPLICATION->form_validation->required($post['bank_state']))
	{
		$errors[] = lang('term_empty') . "&nbsp;" . lang('term_bank_state');
		$fields[] = "bank_state";
	}
	if(!$this->APPLICATION->form_validation->integer($post['bank_country']) || $this->APPLICATION->form_validation->less_than($post['bank_country'],1))
	{
		$errors[] = lang('term_empty') . "&nbsp;" . lang('term_bank_country');
		$fields[] = "bank_country";
	}
	if(!$this->APPLICATION->form_validation->required($post['swift_code']))
	{
		$errors[] = lang('term_empty') . "&nbsp;" . lang('term_swift_code');
		$fields[] = "swift_code";
	}
	if(!$this->APPLICATION->form_validation->required($post['account_number']))
	{
		$errors[] = lang('term_empty') . "&nbsp;" . lang('term_account_number');
		$fields[] = "account_number";
	}
	if(!$this->APPLICATION->form_validation->required($post['account_holder']))
	{
		$errors[] = lang('term_empty') . "&nbsp;" . lang('term_account_holder');
		$fields[] = "account_holder";
	}
	
	// ###########################
	// COMPANY BILLING INFORMATION
	// ###########################	
	if(!$this->APPLICATION->form_validation->required($post['company_balance']))
	{
		$errors[] = lang('term_empty') . "&nbsp;" . lang('term_company_balance');
		$fields[] = "company_balance";
	}
	if(!$this->APPLICATION->form_validation->decimal($post['company_balance']))
	{
		$errors[] = lang('msg_company_balance_can_contain_decimal_numbers_only');
		$fields[] = "company_balance";	
	}
	if(!$this->APPLICATION->form_validation->required($post['company_credit_limit']))
	{
		$errors[] = lang('term_empty') . "&nbsp;" . lang('term_company_credit_limit');
		$fields[] = "company_credit_limit";
	}
	if(!$this->APPLICATION->form_validation->decimal($post['company_credit_limit']))
	{
		$errors[] = lang('msg_company_credit_limit_can_contain_decimal_numbers_only');
		$fields[] = "company_credit_limit";	
	}			
	if(!$this->APPLICATION->form_validation->required($post['company_billing_period']))
	{
		$errors[] = lang('term_empty') . "&nbsp;" . lang('term_company_billing_period');
		$fields[] = "company_billing_period";
	}
	if(!$this->APPLICATION->form_validation->required($post['company_payment_delay']))
	{
		$errors[] = lang('term_empty') . "&nbsp;" . lang('term_company_payment_delay');
		$fields[] = "company_payment_delay";
	}
	if($this->APPLICATION->form_validation->less_than($post['company_payment_delay'],0) )
	{
		$errors[] = lang('msg_company_payment_delay_cannot_be_negative');
		$fields[] = "company_payment_delay";
	}	
	if(!$this->APPLICATION->form_validation->integer($post['company_payment_delay']))
	{
		$errors[] = lang('msg_company_payment_delay_cannot_must_be_integer');
		$fields[] = "company_payment_delay";
	}			
	// #####################
	// OTHER SPECIFIC FIELDS
	// #####################
	if(!$this->APPLICATION->form_validation->required($post['rates_buy_emails']))
	{
		$errors[] = lang('term_empty') . "&nbsp;" . lang('term_rates_buy_emails');
		$fields[] = "rates_buy_emails";
	}		
	if(!$this->APPLICATION->form_validation->valid_emails($post['rates_buy_emails']))
	{
		$errors[] = lang('term_invalid') . "&nbsp;" . lang('term_rates_buy_emails');
		$fields[] = "rates_buy_emails";
	}			
	if(!$this->APPLICATION->form_validation->required($post['timezone_buy']))
	{
		$errors[] = lang('term_empty') . "&nbsp;" . lang('term_timezone_buy');
		$fields[] = "time-zone-buy-select";
	}	
	}

   /*
    * *****************************************************************************
    * *****************************************************************************
	* VERIFICATIONS FOR "WE SELL TO THEM" CONTRACT TYPE
	* *****************************************************************************
	* *****************************************************************************
	*/	
	// ###########################
	// CARRIER BILLING INFORMATION
	// ###########################
	if($post['contract_type'] == "we_sell_to_them" || $post['contract_type'] == "bi_lateral")
	{
	if(!$this->APPLICATION->form_validation->required($post['carrier_balance']))
	{
		$errors[] = lang('term_empty') . "&nbsp;" . lang('term_carrier_balance');
		$fields[] = "carrier_balance";
	}
	if(!$this->APPLICATION->form_validation->decimal($post['carrier_balance']))
	{
		$errors[] = lang('msg_carrier_balance_can_contain_decimal_numbers_only');
		$fields[] = "carrier_balance";	
	}	
	if(!$this->APPLICATION->form_validation->required($post['carrier_credit_limit']))
	{
		$errors[] = lang('term_empty') . "&nbsp;" . lang('term_carrier_credit_limit');
		$fields[] = "carrier_credit_limit";
	}
	if(!$this->APPLICATION->form_validation->decimal($post['carrier_credit_limit']))
	{
		$errors[] = lang('msg_carrier_credit_limit_can_contain_decimal_numbers_only');
		$fields[] = "carrier_credit_limit";	
	}		
	if(!$this->APPLICATION->form_validation->required($post['carrier_billing_period']))
	{
		$errors[] = lang('term_empty') . "&nbsp;" . lang('term_carrier_billing_period');
		$fields[] = "carrier_billing_period";
	}
	if(!$this->APPLICATION->form_validation->required($post['carrier_payment_delay']))
	{
		$errors[] = lang('term_empty') . "&nbsp;" . lang('term_carrier_payment_delay');
		$fields[] = "carrier_payment_delay";
	}	
	if(!$this->APPLICATION->form_validation->integer($post['carrier_payment_delay']))
	{
		$errors[] = lang('msg_carrier_payment_delay_cannot_must_be_integer');
		$fields[] = "carrier_payment_delay";
	}		
	if($this->APPLICATION->form_validation->less_than($post['carrier_payment_delay'],0))
	{
		$errors[] = lang('msg_carrier_payment_delay_cannot_be_negative');
		$fields[] = "carrier_payment_delay";
	}	
	
	// #####################
	// OTHER SPECIFIC FIELDS
	// #####################
	if(!$this->APPLICATION->form_validation->required($post['rates_sell_emails']))
	{
		$errors[] = lang('term_empty') . "&nbsp;" . lang('term_rates_sell_emails');
		$fields[] = "rates_sell_emails";
	}	
	if(!$this->APPLICATION->form_validation->valid_emails($post['rates_sell_emails']))
	{
		$errors[] = lang('term_invalid') . "&nbsp;" . lang('term_rates_sell_emails');
		$fields[] = "rates_sell_emails";
	}		
	if(!$this->APPLICATION->form_validation->required($post['timezone_sell']))
	{
		$errors[] = lang('term_empty') . "&nbsp;" . lang('term_timezone_sell');
		$fields[] = "time-zone-sell-select";
	}	
	}
	
   /*
	* *****************************************************************************
	* THERE ARE SOME ERRORS IN THE FORM - DO NOT PERFORM DATABASE STORING
	* *****************************************************************************
	*/
	if(count($errors) > 0)
	{
		$errors = implode('</li><li>',$errors);
		$fields = implode('**',$fields);
		
		$html = lang('msg_following_errors_occured') . "<br />";
		$html .= "<ul><li>" . $errors . "</li></ul>";
		
		$response = "false|" .  $html . "|" . $fields;
	}
	elseif(count($errors) == 0)
	{
	
		unset($post['confirm']);
		unset($post['password_repeat']);
		
		foreach($post as $variable => $value)
		{
			$$variable = $value;
		}
		
		// reset some variables
		$password = md5($password);
		$status = 1;
		$account_manager_id = 0;
		$account_manager_assignment_date = NULL;

		if($contract_type == "bi_lateral")
		{
		$sql = "INSERT INTO `carriers` (`id`,
										`contract_type`, 
										`username`, 
										`password`, 
										`status`, 
										`create_date`, 
										`special_numbers_only`, 
										`legal_name`, 
										`address_line_1`, 
										`address_line_2`, 
										`city`, 
										`state`, 
										`zip`, 
										`country_id`,
										`registration_id`,
										`vat_id`,
										`website`, 
										`company_phone`, 
										`currency_id`, 
										`timezone_buy`, 
										`timezone_sell`, 
										`account_manager_id`, 
										`account_manager_assignment_date`, 
										`rates_buy_emails`, 
										`rates_sell_emails`, 
										`sales_name`, 
										`sales_emails`, 
										`sales_phone`, 
										`billing_name`, 
										`billing_emails`, 
										`noc_name`, 
										`noc_emails`, 
										`noc_phone`, 
										`legal_emails`, 
										`bank_name`, 
										`bank_address_1`, 
										`bank_address_2`, 
										`bank_city`, 
										`bank_state`, 
										`bank_country`, 
										`swift_code`, 
										`account_number`, 
										`account_holder`, 
										`carrier_balance`, 
										`carrier_credit_limit`, 
										`company_balance`, 
										`company_credit_limit`, 
										`carrier_billing_period`, 
										`carrier_payment_delay`, 
										`company_billing_period`, 
										`company_payment_delay`) 
										VALUES (
										NULL, 
										'{$contract_type}', 
										'{$username}', 
										'{$password}', 
										'{$status}', 
										'CURRENT_TIMESTAMP', 
										'{$special_numbers_only}', 
										'{$legal_name}', 
										'{$address_line_1}', 
										'{$address_line_2}', 
										'{$city}', 
										'{$state}', 
										'{$zip}', 
										'{$country_id}', 
										'{$registration_id}', 
										'{$vat_id}', 
										'{$website}', 
										'{$company_phone}', 
										'{$currency_id}', 
										'{$timezone_buy}', 
										'{$timezone_sell}', 
										'{$account_manager_id}', 
										'{$account_manager_assignment_date}', 
										'{$rates_buy_emails}', 
										'{$rates_sell_emails}', 
										'{$sales_name}', 
										'{$sales_emails}', 
										'{$sales_phone}', 
										'{$billing_name}', 
										'{$billing_emails}', 
										'{$noc_name}', 
										'{$noc_emails}', 
										'{$noc_phone}',
										'{$legal_emails}', 
										'{$bank_name}', 
										'{$bank_address_1}', 
										'{$bank_address_2}', 
										'{$bank_city}', 
										'{$bank_state}', 
										'{$bank_country}', 
										'{$swift_code}', 
										'{$account_number}', 
										'{$account_holder}', 
										'{$carrier_balance}', 
										'{$carrier_credit_limit}', 
										'{$company_balance}', 
										'{$company_credit_limit}', 
										'{$carrier_billing_period}', 
										'{$carrier_payment_delay}', 
										'{$company_billing_period}', 
										'{$company_payment_delay}');";
		}
		
		elseif ($contract_type == "we_buy_from_them")
		{
		$sql = "INSERT INTO `carriers` (`id`,
										`contract_type`, 
										`username`, 
										`password`, 
										`status`, 
										`create_date`, 
										`special_numbers_only`, 
										`legal_name`, 
										`address_line_1`, 
										`address_line_2`, 
										`city`, 
										`state`, 
										`zip`, 
										`country_id`,
										`registration_id`,
										`vat_id`,
										`website`, 
										`company_phone`, 
										`currency_id`, 
										`timezone_buy`, 
										`account_manager_id`, 
										`account_manager_assignment_date`, 
										`rates_buy_emails`, 
										`sales_name`, 
										`sales_emails`, 
										`sales_phone`, 
										`billing_name`, 
										`billing_emails`, 
										`noc_name`, 
										`noc_emails`, 
										`noc_phone`, 
										`legal_emails`, 
										`bank_name`, 
										`bank_address_1`, 
										`bank_address_2`, 
										`bank_city`, 
										`bank_state`, 
										`bank_country`, 
										`swift_code`, 
										`account_number`, 
										`account_holder`, 
										`company_balance`, 
										`company_credit_limit`, 
										`company_billing_period`, 
										`company_payment_delay`) 
										VALUES (
										NULL, 
										'{$contract_type}', 
										'{$username}', 
										'{$password}', 
										'{$status}', 
										'CURRENT_TIMESTAMP', 
										'{$special_numbers_only}', 
										'{$legal_name}', 
										'{$address_line_1}', 
										'{$address_line_2}', 
										'{$city}', 
										'{$state}', 
										'{$zip}', 
										'{$country_id}', 
										'{$registration_id}', 
										'{$vat_id}', 
										'{$website}', 
										'{$company_phone}', 
										'{$currency_id}', 
										'{$timezone_buy}', 
										'{$account_manager_id}', 
										'{$account_manager_assignment_date}', 
										'{$rates_buy_emails}', 
										'{$sales_name}', 
										'{$sales_emails}', 
										'{$sales_phone}', 
										'{$billing_name}', 
										'{$billing_emails}', 
										'{$noc_name}', 
										'{$noc_emails}', 
										'{$noc_phone}',
										'{$legal_emails}', 
										'{$bank_name}', 
										'{$bank_address_1}', 
										'{$bank_address_2}', 
										'{$bank_city}', 
										'{$bank_state}', 
										'{$bank_country}', 
										'{$swift_code}', 
										'{$account_number}', 
										'{$account_holder}', 
										'{$company_balance}', 
										'{$company_credit_limit}', 
										'{$company_billing_period}', 
										'{$company_payment_delay}');";		
		}
		
		elseif ($contract_type == "we_sell_to_them")
		{
		$sql = "INSERT INTO `carriers` (`id`,
										`contract_type`, 
										`username`, 
										`password`, 
										`status`, 
										`create_date`, 
										`special_numbers_only`, 
										`legal_name`, 
										`address_line_1`, 
										`address_line_2`, 
										`city`, 
										`state`, 
										`zip`, 
										`country_id`,
										`registration_id`,
										`vat_id`,
										`website`, 
										`company_phone`, 
										`currency_id`, 
										`timezone_sell`, 
										`account_manager_id`, 
										`account_manager_assignment_date`, 
										`rates_sell_emails`, 
										`sales_name`, 
										`sales_emails`, 
										`sales_phone`, 
										`billing_name`, 
										`billing_emails`, 
										`noc_name`, 
										`noc_emails`,
										`noc_phone`, 
										`legal_emails`, 
										`carrier_balance`, 
										`carrier_credit_limit`, 
										`carrier_billing_period`, 
										`carrier_payment_delay`) 
										VALUES (
										NULL, 
										'{$contract_type}', 
										'{$username}', 
										'{$password}', 
										'{$status}', 
										'CURRENT_TIMESTAMP', 
										'{$special_numbers_only}', 
										'{$legal_name}', 
										'{$address_line_1}', 
										'{$address_line_2}', 
										'{$city}', 
										'{$state}', 
										'{$zip}', 
										'{$country_id}', 
										'{$registration_id}', 
										'{$vat_id}', 
										'{$website}', 
										'{$company_phone}', 
										'{$currency_id}', 
										'{$timezone_sell}', 
										'{$account_manager_id}', 
										'{$account_manager_assignment_date}', 
										'{$rates_sell_emails}', 
										'{$sales_name}', 
										'{$sales_emails}', 
										'{$sales_phone}', 
										'{$billing_name}', 
										'{$billing_emails}', 
										'{$noc_name}', 
										'{$noc_emails}', 
										'{$noc_phone}',
										'{$legal_emails}', 
										'{$carrier_balance}', 
										'{$carrier_credit_limit}', 
										'{$carrier_billing_period}', 
										'{$carrier_payment_delay}');";
		}
	$this->db->query($sql);
	
	$response = "true|" .  lang('msg_carrier_added_successfully');										
	}	
		
	return $response;
}


public function edit_carrier($post,$carrier_id)
{
	$errors = array();
	$fields = array();

   /*
    * *****************************************************************************
    * *****************************************************************************
	* VERIFICATIONS COMMON FOR ALL CONTRACT TYPES
	* *****************************************************************************
	* *****************************************************************************
	*/	
	if(empty($post['contract_type']))
	{
		$errors[] = lang('term_empty') . "&nbsp;" . lang('term_contract_type');
		$fields[] = "contract_type";
	}
	
	// ###################
	// PASSWORD VALIDATION
	// ###################	
	if($post['password'] != $post['password_repeat'])
	{
		$errors[] = lang('msg_passwords_do_not_match');		
		$fields[] = "password**password_repeat";
	}
	if(!empty($post['password']) && strlen($post['password']) < 8)
	{
		$errors[] = lang('msg_password_shorter_than_8');		
		$fields[] = "password**password_repeat";
	}	
	// ###################
	// OTHER GLOBAL FIELDS
	// ###################	
	if(!$this->APPLICATION->form_validation->required($post['legal_name']))
	{
		$errors[] = lang('term_empty') . "&nbsp;" . lang('term_legal_name');	
		$fields[] = "legal_name";
	}	
	if(!$this->APPLICATION->form_validation->required($post['sales_name']))
	{
		$errors[] = lang('term_empty') . "&nbsp;" . lang('term_sales_name');	
		$fields[] = "sales_name";
	}
	if(!$this->APPLICATION->form_validation->required($post['sales_emails']))
	{
		$errors[] = lang('term_empty') . "&nbsp;" . lang('term_sales_emails');
		$fields[] = "sales_emails";		
	}
	if(!$this->APPLICATION->form_validation->valid_emails($post['sales_emails']))
	{
		$errors[] = lang('term_invalid') . "&nbsp;" . lang('term_sales_emails');
		$fields[] = "sales_emails";		
	}	
	if(!$this->APPLICATION->form_validation->required($post['sales_phone']))
	{
		$errors[] = lang('term_empty') . "&nbsp;" . lang('term_sales_phone');
		$fields[] = "sales_phone";		
	}
	if(!$this->APPLICATION->form_validation->required($post['billing_name']))
	{
		$errors[] = lang('term_empty') . "&nbsp;" . lang('term_billing_name');
		$fields[] = "billing_name";
	}
	if(!$this->APPLICATION->form_validation->required($post['billing_emails']))
	{
		$errors[] = lang('term_empty') . "&nbsp;" . lang('term_billing_emails');
		$fields[] = "billing_emails";
	}
	if(!$this->APPLICATION->form_validation->valid_emails($post['billing_emails']))
	{
		$errors[] = lang('term_invalid') . "&nbsp;" . lang('term_billing_emails');
		$fields[] = "billing_emails";
	}	
	if(!$this->APPLICATION->form_validation->required($post['noc_name']))
	{
		$errors[] = lang('term_empty') . "&nbsp;" . lang('term_noc_name');
		$fields[] = "noc_name";
	}
	if(!$this->APPLICATION->form_validation->required($post['noc_emails']))
	{
		$errors[] = lang('term_empty') . "&nbsp;" . lang('term_noc_emails');
		$fields[] = "noc_emails";
	}
	if(!$this->APPLICATION->form_validation->valid_emails($post['noc_emails']))
	{
		$errors[] = lang('term_invalid') . "&nbsp;" . lang('term_noc_emails');
		$fields[] = "noc_emails";
	}	
	if(!$this->APPLICATION->form_validation->required($post['noc_phone']))
	{
		$errors[] = lang('term_empty') . "&nbsp;" . lang('term_noc_phone');
		$fields[] = "noc_phone";
	}
	if(!$this->APPLICATION->form_validation->required($post['legal_emails']))
	{
		$errors[] = lang('term_empty') . "&nbsp;" . lang('term_legal_emails');
		$fields[] = "legal_emails";
	}
	if(!$this->APPLICATION->form_validation->valid_emails($post['legal_emails']))
	{
		$errors[] = lang('term_invalid') . "&nbsp;" . lang('term_legal_emails');
		$fields[] = "legal_emails";
	}	
	if(!$this->APPLICATION->form_validation->required($post['company_phone']))
	{
		$errors[] = lang('term_empty') . "&nbsp;" . lang('term_company_phone');
		$fields[] = "company_phone";
	}
	if(!$this->APPLICATION->form_validation->required($post['currency_id']) || $post['currency_id'] == "none")
	{
		$errors[] = lang('term_empty') . "&nbsp;" . lang('term_currency');	
		$fields[] = "currency";
	}
	if(!$this->APPLICATION->form_validation->required($post['address_line_1']))
	{
		$errors[] = lang('term_empty') . "&nbsp;" . lang('term_address_line_1');		
		$fields[] = "address_line_1";
	}
	if(!$this->APPLICATION->form_validation->required($post['state']))
	{
		$errors[] = lang('term_empty') . "&nbsp;" . lang('term_state');
		$fields[] = "state";
	}
	if(!$this->APPLICATION->form_validation->required($post['city']))
	{
		$errors[] = lang('term_empty') . "&nbsp;" . lang('term_city');	
		$fields[] = "city";
	}
	if(!$this->APPLICATION->form_validation->required($post['zip']))
	{
		$errors[] = lang('term_empty') . "&nbsp;" . lang('term_zip');	
		$fields[] = "zip";
	}
	if(!$this->APPLICATION->form_validation->required($post['registration_id']))
	{
		$errors[] = lang('term_empty') . "&nbsp;" . lang('term_registration_id');	
		$fields[] = "registration_id";
	}
	if(!$this->APPLICATION->form_validation->required($post['vat_id']))
	{
		$errors[] = lang('term_empty') . "&nbsp;" . lang('term_vat_id');	
		$fields[] = "vat_id";
	}	
	if(!$this->APPLICATION->form_validation->integer($post['country_id']) || $this->APPLICATION->form_validation->less_than($post['country_id'],1))
	{
		$errors[] = lang('term_empty') . "&nbsp;" . lang('term_country');	
		$fields[] = "country";
	}

	
   /*
    * *****************************************************************************
    * *****************************************************************************
	* VERIFICATIONS FOR "WE BUY FROM THEM" CONTRACT TYPE
	* *****************************************************************************
	* *****************************************************************************
	*/	
	// ###################
	// BANKING INFORMATION
	// ###################	
	if($post['contract_type'] == "we_buy_from_them" || $post['contract_type'] == "bi_lateral")
	{
	if(!$this->APPLICATION->form_validation->required($post['bank_name']))
	{
		$errors[] = lang('term_empty') . "&nbsp;" . lang('term_bank_name');
		$fields[] = "bank_name";
	}
	if(!$this->APPLICATION->form_validation->required($post['bank_address_1']))
	{
		$errors[] = lang('term_empty') . "&nbsp;" . lang('term_bank_address_1');
		$fields[] = "bank_address_1";
	}
	if(!$this->APPLICATION->form_validation->required($post['bank_city']))
	{
		$errors[] = lang('term_empty') . "&nbsp;" . lang('term_bank_city');
		$fields[] = "bank_city";
	}
	if(!$this->APPLICATION->form_validation->required($post['bank_state']))
	{
		$errors[] = lang('term_empty') . "&nbsp;" . lang('term_bank_state');
		$fields[] = "bank_state";
	}
	if(!$this->APPLICATION->form_validation->integer($post['bank_country']) || $this->APPLICATION->form_validation->less_than($post['bank_country'],1))
	{
		$errors[] = lang('term_empty') . "&nbsp;" . lang('term_bank_country');
		$fields[] = "bank_country";
	}
	if(!$this->APPLICATION->form_validation->required($post['swift_code']))
	{
		$errors[] = lang('term_empty') . "&nbsp;" . lang('term_swift_code');
		$fields[] = "swift_code";
	}
	if(!$this->APPLICATION->form_validation->required($post['account_number']))
	{
		$errors[] = lang('term_empty') . "&nbsp;" . lang('term_account_number');
		$fields[] = "account_number";
	}
	if(!$this->APPLICATION->form_validation->required($post['account_holder']))
	{
		$errors[] = lang('term_empty') . "&nbsp;" . lang('term_account_holder');
		$fields[] = "account_holder";
	}
	
	// ###########################
	// COMPANY BILLING INFORMATION
	// ###########################	
	if(!$this->APPLICATION->form_validation->required($post['company_balance']))
	{
		$errors[] = lang('term_empty') . "&nbsp;" . lang('term_company_balance');
		$fields[] = "company_balance";
	}
	if(!$this->APPLICATION->form_validation->decimal($post['company_balance']))
	{
		$errors[] = lang('msg_company_balance_can_contain_decimal_numbers_only');
		$fields[] = "company_balance";	
	}
	if(!$this->APPLICATION->form_validation->required($post['company_credit_limit']))
	{
		$errors[] = lang('term_empty') . "&nbsp;" . lang('term_company_credit_limit');
		$fields[] = "company_credit_limit";
	}
	if(!$this->APPLICATION->form_validation->decimal($post['company_credit_limit']))
	{
		$errors[] = lang('msg_company_credit_limit_can_contain_decimal_numbers_only');
		$fields[] = "company_credit_limit";	
	}			
	if(!$this->APPLICATION->form_validation->required($post['company_billing_period']))
	{
		$errors[] = lang('term_empty') . "&nbsp;" . lang('term_company_billing_period');
		$fields[] = "company_billing_period";
	}
	if(!$this->APPLICATION->form_validation->required($post['company_payment_delay']))
	{
		$errors[] = lang('term_empty') . "&nbsp;" . lang('term_company_payment_delay');
		$fields[] = "company_payment_delay";
	}
	if($this->APPLICATION->form_validation->less_than($post['company_payment_delay'],0) )
	{
		$errors[] = lang('msg_company_payment_delay_cannot_be_negative');
		$fields[] = "company_payment_delay";
	}	
	if(!$this->APPLICATION->form_validation->integer($post['company_payment_delay']))
	{
		$errors[] = lang('msg_company_payment_delay_cannot_must_be_integer');
		$fields[] = "company_payment_delay";
	}			
	// #####################
	// OTHER SPECIFIC FIELDS
	// #####################
	if(!$this->APPLICATION->form_validation->required($post['rates_buy_emails']))
	{
		$errors[] = lang('term_empty') . "&nbsp;" . lang('term_rates_buy_emails');
		$fields[] = "rates_buy_emails";
	}		
	if(!$this->APPLICATION->form_validation->valid_emails($post['rates_buy_emails']))
	{
		$errors[] = lang('term_invalid') . "&nbsp;" . lang('term_rates_buy_emails');
		$fields[] = "rates_buy_emails";
	}			
	if(!$this->APPLICATION->form_validation->required($post['timezone_buy']))
	{
		$errors[] = lang('term_empty') . "&nbsp;" . lang('term_timezone_buy');
		$fields[] = "time-zone-buy-select";
	}	
	}

   /*
    * *****************************************************************************
    * *****************************************************************************
	* VERIFICATIONS FOR "WE SELL TO THEM" CONTRACT TYPE
	* *****************************************************************************
	* *****************************************************************************
	*/	
	// ###########################
	// CARRIER BILLING INFORMATION
	// ###########################
	if($post['contract_type'] == "we_sell_to_them" || $post['contract_type'] == "bi_lateral")
	{
	if(!$this->APPLICATION->form_validation->required($post['carrier_balance']))
	{
		$errors[] = lang('term_empty') . "&nbsp;" . lang('term_carrier_balance');
		$fields[] = "carrier_balance";
	}
	if(!$this->APPLICATION->form_validation->decimal($post['carrier_balance']))
	{
		$errors[] = lang('msg_carrier_balance_can_contain_decimal_numbers_only');
		$fields[] = "carrier_balance";	
	}	
	if(!$this->APPLICATION->form_validation->required($post['carrier_credit_limit']))
	{
		$errors[] = lang('term_empty') . "&nbsp;" . lang('term_carrier_credit_limit');
		$fields[] = "carrier_credit_limit";
	}
	if(!$this->APPLICATION->form_validation->decimal($post['carrier_credit_limit']))
	{
		$errors[] = lang('msg_carrier_credit_limit_can_contain_decimal_numbers_only');
		$fields[] = "carrier_credit_limit";	
	}		
	if(!$this->APPLICATION->form_validation->required($post['carrier_billing_period']))
	{
		$errors[] = lang('term_empty') . "&nbsp;" . lang('term_carrier_billing_period');
		$fields[] = "carrier_billing_period";
	}
	if(!$this->APPLICATION->form_validation->required($post['carrier_payment_delay']))
	{
		$errors[] = lang('term_empty') . "&nbsp;" . lang('term_carrier_payment_delay');
		$fields[] = "carrier_payment_delay";
	}	
	if(!$this->APPLICATION->form_validation->integer($post['carrier_payment_delay']))
	{
		$errors[] = lang('msg_carrier_payment_delay_cannot_must_be_integer');
		$fields[] = "carrier_payment_delay";
	}		
	if($this->APPLICATION->form_validation->less_than($post['carrier_payment_delay'],0))
	{
		$errors[] = lang('msg_carrier_payment_delay_cannot_be_negative');
		$fields[] = "carrier_payment_delay";
	}	
	
	// #####################
	// OTHER SPECIFIC FIELDS
	// #####################
	if(!$this->APPLICATION->form_validation->required($post['rates_sell_emails']))
	{
		$errors[] = lang('term_empty') . "&nbsp;" . lang('term_rates_sell_emails');
		$fields[] = "rates_sell_emails";
	}	
	if(!$this->APPLICATION->form_validation->valid_emails($post['rates_sell_emails']))
	{
		$errors[] = lang('term_invalid') . "&nbsp;" . lang('term_rates_sell_emails');
		$fields[] = "rates_sell_emails";
	}		
	if(!$this->APPLICATION->form_validation->required($post['timezone_sell']))
	{
		$errors[] = lang('term_empty') . "&nbsp;" . lang('term_timezone_sell');
		$fields[] = "time-zone-sell-select";
	}	
	}
	
   /*
	* *****************************************************************************
	* THERE ARE SOME ERRORS IN THE FORM - DO NOT PERFORM DATABASE STORING
	* *****************************************************************************
	*/
	if(count($errors) > 0)
	{
		$errors = implode('</li><li>',$errors);
		$fields = implode('**',$fields);
		
		$html = lang('msg_following_errors_occured') . "<br />";
		$html .= "<ul><li>" . $errors . "</li></ul>";
		
		$response = "false|" .  $html . "|" . $fields;
	}
	elseif(count($errors) == 0)
	{
	
		unset($post['confirm']);
		unset($post['password_repeat']);
		
		foreach($post as $variable => $value)
		{
			$update[$variable] = $value;
		}
		
		// reset password only when new password entered
		if(empty($post['password']))
		{
		unset($update['password']);
		}
		
		// never change username
		unset($update['username']);
		
		/*
		$update['status'] = 1;
		$update['account_manager_id'] = 0;
		$update['account_manager_assignment_date'] = NULL;
		*/
		
		if($update['contract_type'] == "bi_lateral")
		{
		}
		
		elseif ($update['contract_type'] == "we_buy_from_them")
		{
			$update['timezone_sell'] = NULL;
			$update['rates_sell_emails'] = NULL;
			$update['carrier_balance'] = NULL;
			$update['carrier_credit_limit'] = NULL;
			$update['carrier_billing_period'] = NULL;
			$update['carrier_payment_delay'] = NULL;
		}
		elseif ($update['contract_type'] == "we_sell_to_them")
		{
			$update['timezone_buy'] = NULL;
			$update['rates_buy_emails'] = NULL;
			$update['company_balance'] = NULL;
			$update['company_credit_limit'] = NULL;
			$update['company_billing_period'] = NULL;
			$update['company_payment_delay'] = NULL;
			$update['bank_name'] = NULL;
			$update['bank_address_1'] = NULL;
			$update['bank_address_2'] = NULL;
			$update['bank_city'] = NULL;
			$update['bank_state'] = NULL;
			$update['bank_country'] = NULL;
			$update['swift_code'] = NULL;
			$update['account_number'] = NULL;
			$update['account_holder'] = NULL;		
		}
		
	$sql = "UPDATE  `{$this->MODULE->mysql_table}` SET ";
	
	$array = array();
	foreach($update as $name => $value)
	{
		$array[] = " `" . $name . "`" . " = " . "'" . $value . "' ";
	}	
	$sql_part = implode(" , ",$array);
	
	$sql .= $sql_part;
	
	$sql .= "WHERE  `{$this->MODULE->mysql_table}`.`id` ={$carrier_id} LIMIT 1 ;";
	
	$this->db->query($sql);
	
	$response = "true|" .  lang('msg_carrier_updated_successfully');										
	}	
		
	return $response;
}	
}
?>
